package com.project.web.wechat.controller.app.cpWeChat;

import com.project.common.core.utils.exception.CustomException;
import com.project.common.core.utils.exception.Result;
import com.project.web.config.WxCorp.WxCpConfig;
import com.project.web.config.WxCorp.WxCpConfiguration;
import com.project.web.wechat.controller.common.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpAgentService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpAgent;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 应用管理
 *
 * @author wyy
 * @date 2020-06-24
 */
@Api(tags = "应用管理接口")
@RestController("workWxAgentController")
@RequestMapping("/weChat/auth/workWx/agent")
public class WorkWxAgentController extends BaseController {

    /**
     * 根据应用ID查询应用
     *
     * @return
     */
    @ApiOperation("根据应用ID查询应用")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "agentId", value = "应用ID", required = true, dataType = "String"),
    })
    @GetMapping(value = "/getAgent")
    public Result<WxCpAgent> getAgentInfo(HttpServletRequest request) {

        // 企业微信基本参数
        WxCpConfig wxCpConfig = getWxCpConfig(request);
        WxCpService wxCpService = WxCpConfiguration.getCpService(wxCpConfig.getCorpId(), wxCpConfig.getAgentId());
        WxCpAgentService agentService = wxCpService.getAgentService();
        try {
            WxCpAgent wxCpAgent = agentService.get(Integer.valueOf(wxCpConfig.getAgentId()));
            return getResult(wxCpAgent);
        } catch (WxErrorException e) {
            log.error("获取应用信息出错：{}", ExceptionUtils.getStackTrace(e));
            throw new CustomException(String.valueOf(e.getError().getErrorCode()), e.getError().getErrorMsg());
        }
    }

    /**
     * 设置应用信息
     *
     * @param wxCpAgent 应用对象
     * @return
     */
    @ApiOperation("设置应用")
    @PostMapping(value = "/setAgent")
    public Result<WxCpAgent> setAgent(WxCpAgent wxCpAgent, HttpServletRequest request) {

        // 企业微信基本参数
        WxCpConfig wxCpConfig = getWxCpConfig(request);
        WxCpService wxCpService = WxCpConfiguration.getCpService(wxCpConfig.getCorpId(), wxCpConfig.getAgentId());
        WxCpAgentService agentService = wxCpService.getAgentService();
        try {
            agentService.set(wxCpAgent);
            return getResult(wxCpAgent);
        } catch (WxErrorException e) {
            log.error("设置应用信息出错：{}", ExceptionUtils.getStackTrace(e));
            throw new CustomException(String.valueOf(e.getError().getErrorCode()), e.getError().getErrorMsg());
        }
    }
}
